public class MyLinkedList implements IList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int data) {
            this.val = data;
        }
    }
    public ListNode head;
    public ListNode last;
    /**
     * 打印链表
     */
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    /**
     * 求节点个数
     */
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    /**
     * 判断元素是否存在
     */
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    /**
     * 头插法
     */
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    /**
     * 尾插
     */
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if(head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    /**
     * 任意位置插入
     */
    public void addIndex(int index,int data) {
        //判断是否合法
        if(index < 0 || index > size()) {
            throw new IndexException("index下标有误,下标为: " + index);
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size() - 1) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        //先后再前
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    /**
     * 删除节点
     */
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                //删除头
                if(cur == head) {//删除节点为头节点
                    head = head.next;
                    if(head != null) {//多个节点
                        head.prev = null;
                    }else {//只有一个节点,且为需要删除的节点
                        last = null;
                    }
                }else {//中间和尾巴节点
                    cur.prev.next = cur.next;
                    if(cur.next == null) {//尾删
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }else {//中间删除
                        cur.next.prev = cur.prev;
                        //cur.prev.next = cur.next;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    /**
     * 清空节点
     */
    public void clear() {
        this.head = null;
        this.last = null;
    }
}
